package com.version1_4.dao.implement;

import com.version1_4.dao.MsgDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by reeveslee on 16/5/7.
 */
@Repository
public class MsgDaoImpl implements MsgDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> topicReplyMsg(int userId, int lastId) {

        if (lastId != 0) {
            String sql = "SELECT topic.id AS topicId,topic.image AS topicImage,topic.title AS topicTitle,topic.secondary AS topicSecondary,feed.text AS feedText,feed.image AS feedImage,feed.voice AS feedVoice,feed_reply.id AS replyId,feed.id AS feedId,user_info.userName AS name,feed_reply.text,feed_reply.voice,feed_reply.time FROM feed_reply,feed,topic,user_info WHERE feed_reply.feedId=feed.id AND feed_reply.userId=user_info.userId AND feed.topicId=topic.id AND feed.user=? AND feed_reply.id<? ORDER BY feed_reply.id DESC LIMIT ?";
            return jdbcTemplate.queryForList(sql, new Object[]{userId, lastId, 20});
        } else {
            String sql = "SELECT topic.id AS topicId,topic.image AS topicImage,topic.title AS topicTitle,topic.secondary AS topicSecondary,feed.text AS feedText,feed.image AS feedImage,feed.voice AS feedVoice,feed_reply.id AS replyId,feed.id AS feedId,user_info.userName AS name,feed_reply.text,feed_reply.voice,feed_reply.time FROM feed_reply,feed,topic,user_info WHERE feed_reply.feedId=feed.id AND feed_reply.userId=user_info.userId AND feed.topicId=topic.id AND feed.user=? ORDER BY feed_reply.id DESC LIMIT ?";
            return jdbcTemplate.queryForList(sql, new Object[]{userId, 20});
        }
    }

    @Override
    public List<Map<String, Object>> topicPraiseMsg(int userId, int lastId) {

        if (lastId != 0) {
            String sql = "SELECT feed_praise.id,feed_praise.time,topic.id AS topicId,topic.image AS topicImage,topic.title AS topicTitle,topic.secondary AS topicSecondary,feed.text AS feedText,feed.image AS feedImage,feed.voice AS feedVoice,feed.id AS feedId,user_info.userName AS name FROM feed_praise,feed,topic,user_info WHERE feed_praise.feedId=feed.id AND feed_praise.userId=user_info.userId AND feed.topicId=topic.id AND feed.user=? AND feed_praise.id<? ORDER BY feed_praise.time DESC LIMIT ?";
            return jdbcTemplate.queryForList(sql, new Object[]{userId, lastId, 20});
        } else {
            String sql = "SELECT feed_praise.id,feed_praise.time,topic.id AS topicId,topic.image AS topicImage,topic.title AS topicTitle,topic.secondary AS topicSecondary,feed.text AS feedText,feed.image AS feedImage,feed.voice AS feedVoice,feed.id AS feedId,user_info.userName AS name FROM feed_praise,feed,topic,user_info WHERE feed_praise.feedId=feed.id AND feed_praise.userId=user_info.userId AND feed.topicId=topic.id AND feed.user=? ORDER BY feed_praise.time DESC LIMIT ?";
            return jdbcTemplate.queryForList(sql, new Object[]{userId, 20});
        }
    }

    @Override
    public Date lastTopicReplyTime(int userId) {
        String sql = "SELECT time FROM feed_reply_read WHERE userId=?";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.util.Date.class);
        } catch (EmptyResultDataAccessException exception) {
            return new Date(1);
        }

    }

    @Override
    public boolean freshLastTopicReplyTime(int userId) {
        String sql = "UPDATE feed_reply_read SET time=? WHERE userId=?";
        int row = jdbcTemplate.update(sql, new Object[]{new Date(), userId});

        if (row == 0) {
            sql = "INSERT INTO feed_reply_read(userId,time) VALUES (?,?)";
            jdbcTemplate.update(sql, new Object[]{userId, new Date()});
        }

        return true;
    }

    @Override
    public Date lastTopicPraiseTime(int userId) {
        String sql = "SELECT time FROM feed_praise_read WHERE userId=?";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{userId}, java.util.Date.class);
        } catch (EmptyResultDataAccessException exception) {
            return new Date(1);
        }
    }

    @Override
    public boolean freshLastTopicPraiseTime(int userId) {
        String sql = "UPDATE feed_praise_read SET time=? WHERE userId=?";
        int row = jdbcTemplate.update(sql, new Object[]{new Date(), userId});

        if (row == 0) {
            sql = "INSERT INTO feed_praise_read(userId,time) VALUES (?,?)";
            jdbcTemplate.update(sql, new Object[]{userId, new Date()});
        }

        return true;
    }
}
